/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.google.common.base.Joiner;
/*     */ import com.zimbra.client.ZConversation;
/*     */ import com.zimbra.client.ZConversation.ZMessageSummary;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.Fetch;
/*     */ import com.zimbra.client.ZMailbox.ZOutgoingMessage;
/*     */ import com.zimbra.client.ZMailbox.ZSendMessageResponse;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZMessageHit;
/*     */ import com.zimbra.client.ZSearchHit;
/*     */ import com.zimbra.client.ZSearchParams;
/*     */ import com.zimbra.client.ZSearchResult;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.Test;
/*     */ import org.junit.runner.RunWith;
/*     */ import org.junit.runners.Parameterized;
/*     */ import org.junit.runners.Parameterized.Parameters;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @RunWith(Parameterized.class)
/*     */ public class TestSearchConv
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String REMOTE_USER_NAME = "user2";
/*  52 */   private static String subject = "TestSearchConv";
/*     */   private static ZMailbox mbox;
/*     */   private static ZMailbox remote_mbox;
/*     */   private static String convId;
/*  56 */   private static ArrayList<String> msgIds = new ArrayList();
/*     */   private static ZConversation conv;
/*     */   private final String query;
/*     */   private final ZMailbox.Fetch fetch;
/*     */   private final int[] expected;
/*     */   private final int[] unread;
/*     */   
/*     */   public TestSearchConv(String query, ZMailbox.Fetch fetch, int[] unread, int[] expected)
/*     */   {
/*  65 */     this.query = query;
/*  66 */     this.fetch = fetch;
/*  67 */     this.expected = expected;
/*  68 */     this.unread = unread;
/*     */   }
/*     */   
/*     */ 
/*     */   @Parameterized.Parameters
/*     */   public static Collection<Object[]> testInputs()
/*     */     throws Exception
/*     */   {
/*  76 */     setUp();
/*  77 */     ArrayList<Object[]> testCases = new ArrayList();
/*     */     
/*     */ 
/*  80 */     ZMailbox.Fetch fetchLastMsg = new ZMailbox.Fetch((String)msgIds.get(3));
/*  81 */     ZMailbox.Fetch fetchFirstAndThirdMsg = new ZMailbox.Fetch((String)msgIds.get(0) + "," + (String)msgIds.get(2));
/*     */     
/*     */ 
/*  84 */     String q = "dungeons or mountains";
/*     */     
/*  86 */     int[] unread = { 0, 1, 2, 3 };
/*  87 */     addTestCase(testCases, q, ZMailbox.Fetch.none, unread, new int[0]);
/*  88 */     addTestCase(testCases, q, ZMailbox.Fetch.all, unread, new int[] { 0, 1, 2, 3 });
/*  89 */     addTestCase(testCases, q, ZMailbox.Fetch.hits, unread, new int[] { 2, 3 });
/*  90 */     addTestCase(testCases, q, ZMailbox.Fetch.first, unread, new int[] { 2 });
/*  91 */     addTestCase(testCases, q, ZMailbox.Fetch.unread, unread, new int[] { 2, 3 });
/*  92 */     addTestCase(testCases, q, ZMailbox.Fetch.u1, unread, new int[] { 2, 3 });
/*  93 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/*  94 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 2, 3 });
/*  95 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 2, 3 });
/*  96 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 2, 3 });
/*  97 */     addTestCase(testCases, q, fetchLastMsg, unread, new int[] { 3 });
/*  98 */     addTestCase(testCases, q, fetchFirstAndThirdMsg, unread, new int[] { 0, 2 });
/*     */     
/*     */ 
/* 101 */     unread = new int[] { 3 };
/* 102 */     addTestCase(testCases, q, ZMailbox.Fetch.none, unread, new int[0]);
/* 103 */     addTestCase(testCases, q, ZMailbox.Fetch.all, unread, new int[] { 0, 1, 2, 3 });
/* 104 */     addTestCase(testCases, q, ZMailbox.Fetch.hits, unread, new int[] { 2, 3 });
/* 105 */     addTestCase(testCases, q, ZMailbox.Fetch.first, unread, new int[] { 2 });
/* 106 */     addTestCase(testCases, q, ZMailbox.Fetch.unread, unread, new int[] { 3 });
/* 107 */     addTestCase(testCases, q, ZMailbox.Fetch.u1, unread, new int[] { 3 });
/* 108 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/* 109 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 2, 3 });
/* 110 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 3 });
/* 111 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 3 });
/* 112 */     addTestCase(testCases, q, fetchLastMsg, unread, new int[] { 3 });
/* 113 */     addTestCase(testCases, q, fetchFirstAndThirdMsg, unread, new int[] { 0, 2 });
/*     */     
/*     */ 
/* 116 */     unread = new int[0];
/* 117 */     addTestCase(testCases, q, ZMailbox.Fetch.none, unread, new int[0]);
/* 118 */     addTestCase(testCases, q, ZMailbox.Fetch.all, unread, new int[] { 0, 1, 2, 3 });
/* 119 */     addTestCase(testCases, q, ZMailbox.Fetch.hits, unread, new int[] { 2, 3 });
/* 120 */     addTestCase(testCases, q, ZMailbox.Fetch.first, unread, new int[] { 2 });
/* 121 */     addTestCase(testCases, q, ZMailbox.Fetch.unread, unread, new int[0]);
/* 122 */     addTestCase(testCases, q, ZMailbox.Fetch.u1, unread, new int[] { 2 });
/* 123 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/* 124 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 2, 3 });
/* 125 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 0 });
/* 126 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 0, 2 });
/* 127 */     addTestCase(testCases, q, fetchLastMsg, unread, new int[] { 3 });
/* 128 */     addTestCase(testCases, q, fetchFirstAndThirdMsg, unread, new int[] { 0, 2 });
/*     */     
/*     */ 
/* 131 */     q = "thiswontmatch";
/* 132 */     unread = new int[0];
/* 133 */     addTestCase(testCases, q, ZMailbox.Fetch.none, unread, new int[0]);
/* 134 */     addTestCase(testCases, q, ZMailbox.Fetch.all, unread, new int[] { 0, 1, 2, 3 });
/* 135 */     addTestCase(testCases, q, ZMailbox.Fetch.hits, unread, new int[0]);
/* 136 */     addTestCase(testCases, q, ZMailbox.Fetch.first, unread, new int[0]);
/* 137 */     addTestCase(testCases, q, ZMailbox.Fetch.unread, unread, new int[0]);
/* 138 */     addTestCase(testCases, q, ZMailbox.Fetch.u1, unread, new int[0]);
/* 139 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/* 140 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 0 });
/* 141 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 0 });
/* 142 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 0 });
/* 143 */     addTestCase(testCases, q, fetchLastMsg, unread, new int[] { 3 });
/* 144 */     addTestCase(testCases, q, fetchFirstAndThirdMsg, unread, new int[] { 0, 2 });
/*     */     
/*     */ 
/*     */ 
/* 148 */     unread = new int[] { 2, 3 };
/* 149 */     addTestCase(testCases, q, ZMailbox.Fetch.none, unread, new int[0]);
/* 150 */     addTestCase(testCases, q, ZMailbox.Fetch.all, unread, new int[] { 0, 1, 2, 3 });
/* 151 */     addTestCase(testCases, q, ZMailbox.Fetch.hits, unread, new int[0]);
/* 152 */     addTestCase(testCases, q, ZMailbox.Fetch.first, unread, new int[0]);
/* 153 */     addTestCase(testCases, q, ZMailbox.Fetch.unread, unread, new int[0]);
/* 154 */     addTestCase(testCases, q, ZMailbox.Fetch.u1, unread, new int[0]);
/* 155 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/* 156 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 0 });
/* 157 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 0 });
/* 158 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 0 });
/* 159 */     addTestCase(testCases, q, fetchLastMsg, unread, new int[] { 3 });
/* 160 */     addTestCase(testCases, q, fetchFirstAndThirdMsg, unread, new int[] { 0, 2 });
/*     */     
/*     */ 
/* 163 */     q = "enchanted or mountains";
/*     */     
/* 165 */     unread = new int[0];
/* 166 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/* 167 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 0, 3 });
/* 168 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 0 });
/* 169 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 0 });
/*     */     
/*     */ 
/* 172 */     unread = new int[] { 0, 1, 2, 3 };
/* 173 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/* 174 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 0, 3 });
/* 175 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 0, 3 });
/* 176 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 0, 3 });
/*     */     
/*     */ 
/* 179 */     unread = new int[] { 2, 3 };
/* 180 */     addTestCase(testCases, q, ZMailbox.Fetch.first_msg, unread, new int[] { 0 });
/* 181 */     addTestCase(testCases, q, ZMailbox.Fetch.hits_or_first_msg, unread, new int[] { 0, 3 });
/* 182 */     addTestCase(testCases, q, ZMailbox.Fetch.u_or_first_msg, unread, new int[] { 3 });
/* 183 */     addTestCase(testCases, q, ZMailbox.Fetch.u1_or_first_msg, unread, new int[] { 3 });
/*     */     
/* 185 */     return testCases;
/*     */   }
/*     */   
/*     */   private static void addTestCase(ArrayList<Object[]> testCases, String query, ZMailbox.Fetch fetch, int[] unread, int[] expected) {
/* 189 */     Object[] args = { query, fetch, unread, expected };
/* 190 */     testCases.add(args);
/*     */   }
/*     */   
/*     */   private static void setupConversation()
/*     */     throws Exception
/*     */   {
/* 196 */     ZMailbox.ZOutgoingMessage msg = TestUtil.getOutgoingMessage("user2", subject, "far over the misty mountains cold", null);
/* 197 */     ZMailbox.ZSendMessageResponse resp = mbox.sendMessage(msg, null, false);
/* 198 */     Thread.sleep(1000L);
/* 199 */     String remoteMsgId = TestUtil.getMessage(remote_mbox, subject).getId();
/* 200 */     ZMailbox.ZOutgoingMessage reply = TestUtil.getOutgoingMessage("user1", subject, "to dungeons deep and caverns old", null);
/* 201 */     reply.setOriginalMessageId(remoteMsgId);
/* 202 */     reply.setReplyType("r");
/* 203 */     remote_mbox.sendMessage(reply, null, false);
/* 204 */     Thread.sleep(1000L);
/* 205 */     reply = TestUtil.getOutgoingMessage("user1", subject, "we must away ere break of day", null);
/* 206 */     reply.setOriginalMessageId(remoteMsgId);
/* 207 */     reply.setReplyType("r");
/* 208 */     remote_mbox.sendMessage(reply, null, false);
/* 209 */     Thread.sleep(1000L);
/* 210 */     reply = TestUtil.getOutgoingMessage("user1", subject, "to seek the pale enchanted gold", null);
/* 211 */     reply.setOriginalMessageId(remoteMsgId);
/* 212 */     reply.setReplyType("r");
/* 213 */     remote_mbox.sendMessage(reply, null, false);
/* 214 */     Thread.sleep(1000L);
/* 215 */     convId = mbox.getMessageById(resp.getId()).getConversationId();
/*     */   }
/*     */   
/*     */   public static void setUp() throws Exception {
/* 219 */     mbox = TestUtil.getZMailbox("user1");
/* 220 */     remote_mbox = TestUtil.getZMailbox("user2");
/* 221 */     setupConversation();
/* 222 */     conv = mbox.getConversation(convId, ZMailbox.Fetch.all);
/* 223 */     List<ZConversation.ZMessageSummary> msgs = conv.getMessageSummaries();
/* 224 */     for (ZConversation.ZMessageSummary msg : msgs) {
/* 225 */       msgIds.add(msg.getId());
/*     */     }
/*     */     
/* 228 */     Collections.reverse(msgIds);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void searchConversation() throws Exception
/*     */   {
/* 234 */     ZimbraLog.search.debug("testing query '%s' with fetch='%s', unread=%s (expecting '%s')", new Object[] { this.query, this.fetch, Arrays.toString(this.unread), Arrays.toString(this.expected) });
/* 235 */     markMessagesUnreadByIndex(this.unread);
/* 236 */     ZSearchParams params = new ZSearchParams(this.query);
/* 237 */     params.setFetch(this.fetch);
/* 238 */     ZSearchResult result = mbox.searchConversation(convId, params);
/* 239 */     List<ZSearchHit> hits = result.getHits();
/* 240 */     List<String> expandedList = new ArrayList();
/* 241 */     int[] arr$ = this.expected;int len$ = arr$.length; for (int i$ = 0; i$ < len$; i$++) { Integer idx = Integer.valueOf(arr$[i$]);
/* 242 */       expandedList.add(msgIds.get(idx.intValue()));
/*     */     }
/* 244 */     for (ZSearchHit hit : hits) {
/* 245 */       boolean expanded = isExpanded(hit);
/* 246 */       if (expandedList.contains(hit.getId())) {
/* 247 */         Assert.assertTrue(expanded);
/* 248 */         expandedList.remove(hit.getId());
/*     */       }
/*     */       else {
/* 251 */         Assert.assertFalse(expanded);
/*     */       }
/*     */     }
/* 254 */     Assert.assertEquals(0L, expandedList.size());
/*     */   }
/*     */   
/*     */   private void markAllMessagesRead() throws Exception {
/* 258 */     mbox.markMessageRead(Joiner.on(",").join(msgIds), true);
/*     */   }
/*     */   
/*     */   private void markMessagesUnreadByIndex(int[] indexes) throws Exception {
/* 262 */     ArrayList<String> ids = new ArrayList();
/* 263 */     for (int i : indexes) {
/* 264 */       ids.add(msgIds.get(i));
/*     */     }
/* 266 */     String commaSeparatedIds = Joiner.on(",").join(ids);
/* 267 */     markAllMessagesRead();
/* 268 */     if (commaSeparatedIds.length() > 0) {
/* 269 */       mbox.markMessageRead(commaSeparatedIds, false);
/*     */     }
/*     */     
/* 272 */     for (String id : msgIds) {
/* 273 */       ZMessage message = mbox.getMessageById(id);
/* 274 */       boolean unread = message.isUnread();
/* 275 */       if (ids.contains(id)) Assert.assertTrue(unread); else {
/* 276 */         Assert.assertFalse(unread);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private boolean isExpanded(ZSearchHit hit) {
/* 282 */     ZMessage msg = ((ZMessageHit)hit).getMessage();
/* 283 */     boolean expanded = msg != null;
/* 284 */     return expanded;
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanUp() throws Exception {
/* 289 */     TestUtil.deleteTestData("user1", subject);
/* 290 */     TestUtil.deleteTestData("user2", subject);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSearchConv.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */